package com.yjhb.android.sbz.adapter.taskmanage;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.ManagerTask;
import com.yjhb.android.sbz.enums.TaskState;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxSpanTool;

import java.util.Date;

/**
 * 创建者 zx
 * 创建时间 2020/12/9 16:51
 */
public class TaskManagerDisplayAdapter extends BaseRecyclerAdapter<TaskManagerDisplayAdapter.Holder, ManagerTask> {
    private final String TAG = "TaskManagerDisplayAdapter";

    public TaskManagerDisplayAdapter(Context context) {
        super(context);
    }

    @Override
    protected Holder getHolder(View v) {
        return new Holder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_task_manager_display;
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int i) {
        final int index = i;
        final ManagerTask entity = data.get(i);
        holder.name_tv.setText(entity.getTask().getName());
        holder.money_tv.setText("（" + entity.getTask().getMoney() + "元）");
        holder.watch_tv.setText("浏览次数:" + entity.getQueryCount() + "次");
        holder.total_tv.setText("悬赏名额：" + entity.getTask().getMaxStock() + "个");
        holder.success_tv.setText("已完成：" + entity.getTask().getSuccess() + "个");
        holder.last_num_tv.setText("" + entity.getTask().getCurrentStock() + "个");
        boolean isStop = entity.getTask().getState() == TaskState.TASK_STATE_STOP.getState();
        if (isStop) {
            holder.stop_tv.setText("开启");

        } else {
            holder.stop_tv.setText("暂停");
        }
        Date endTime = entity.getTask().getCancelHomeTime();
        boolean isRecommend = false;
        if (endTime != null) {
            long current = System.currentTimeMillis();
            long end = endTime.getTime() - current;
            if (end > 0) {
                isRecommend = true;
                long hour = end / 1000 / 60 / 60;
                long min = (end - (hour * (1000 * 60 * 60))) / 1000 / 60;
                holder.time_tv.setText(new StringBuilder("置顶剩余时长:")
                        .append(hour)
                        .append("小时")
                        .append(min)
                        .append("分钟"));
                holder.top_tv.setBackgroundResource(R.drawable.task_manager_button);
                holder.top_tv.setTextColor(Color.WHITE);
                holder.top_tv.setText("置顶中");
                holder.time_tv.setVisibility(View.VISIBLE);
            } else {
                holder.time_tv.setVisibility(View.GONE);
                holder.top_tv.setText("置顶");
                holder.top_tv.setBackgroundResource(R.drawable.frame_bg);
                holder.top_tv.setTextColor(context.getResources().getColor(R.color.colorPrimaryOtherDark));
            }
        } else {
            holder.time_tv.setVisibility(View.GONE);
            holder.top_tv.setText("置顶");
            holder.top_tv.setBackgroundResource(R.drawable.frame_bg);
            holder.top_tv.setTextColor(context.getResources().getColor(R.color.colorPrimaryOtherDark));
        }

        if (entity.getTask().getAuditFast() == 1) {
            holder.fast_tv.setBackgroundResource(R.drawable.task_manager_button);
            holder.fast_tv.setTextColor(Color.WHITE);
            holder.fast_tv.setText("快审中");
        } else {
            holder.fast_tv.setBackgroundResource(R.drawable.frame_bg);
            holder.fast_tv.setTextColor(context.getResources().getColor(R.color.colorPrimaryOtherDark));
            holder.fast_tv.setText("快审");
        }

        CharSequence waitCheck = "待审核(" + entity.getWaitAuditCount() + ")";
        if (entity.getWaitAuditCount() > 0) {
            holder.wait_num_tv.setBackgroundResource(R.drawable.db_btn);
            waitCheck = ZxSpanTool.ColorSpan(waitCheck, 4, 4 + ("" + entity.getWaitAuditCount()).length(),
                    context.getResources().getColor(R.color.white));
        } else {
            holder.wait_num_tv.setBackgroundResource(R.drawable.theme_btn);
        }
        holder.wait_num_tv.setText(waitCheck);
        holder.wait_num_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.wait_num_tv, "check", index, entity);
            }
        });
        holder.top_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.top_tv, "top", index, entity);
            }
        });
        boolean finalIsRecommend = isRecommend;
        holder.refresh_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.refresh_tv, "refresh", index, entity);
            }
        });
        holder.redpack_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.redpack_tv, "redpack", index, entity);
            }
        });
        holder.addmoney_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.addmoney_tv, "addmoney", index, entity);
            }
        });
        holder.count_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.count_tv, "count", index, entity);
            }
        });
        holder.fast_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.fast_tv, "fast", index, entity);
            }
        });
        holder.stop_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.stop_tv, "stop", index, entity);
            }
        });
        holder.refresh_package_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.refresh_package_tv, "refresh_package", index, entity);
            }
        });
        holder.timer_recommend_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.timer_recommend_tv, "timer_recommend", index, entity);
            }
        });
        holder.auto_refresh_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(holder.auto_refresh_tv, "auto_refresh", index, entity);
            }
        });
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView name_tv;
        TextView money_tv;
        TextView watch_tv;
        TextView total_tv;
        TextView success_tv;
        TextView last_num_tv;
        TextView wait_num_tv;
        TextView top_tv;
        TextView refresh_tv;
        TextView redpack_tv;
        TextView addmoney_tv;
        TextView count_tv;
        TextView fast_tv;
        TextView stop_tv;
        TextView time_tv;
        TextView refresh_package_tv;
        TextView timer_recommend_tv;
        TextView auto_refresh_tv;

        public Holder(@NonNull View itemView) {
            super(itemView);
            name_tv = itemView.findViewById(R.id.name_tv);
            money_tv = itemView.findViewById(R.id.money_tv);
            watch_tv = itemView.findViewById(R.id.watch_tv);
            total_tv = itemView.findViewById(R.id.total_tv);
            success_tv = itemView.findViewById(R.id.success_tv);
            last_num_tv = itemView.findViewById(R.id.last_num_tv);
            wait_num_tv = itemView.findViewById(R.id.wait_num_tv);
            top_tv = itemView.findViewById(R.id.top_tv);
            refresh_tv = itemView.findViewById(R.id.refresh_tv);
            redpack_tv = itemView.findViewById(R.id.redpack_tv);
            addmoney_tv = itemView.findViewById(R.id.addmoney_tv);
            count_tv = itemView.findViewById(R.id.count_tv);
            fast_tv = itemView.findViewById(R.id.fast_tv);
            stop_tv = itemView.findViewById(R.id.stop_tv);
            time_tv = itemView.findViewById(R.id.time_tv);
            refresh_package_tv = itemView.findViewById(R.id.refresh_package_tv);
            timer_recommend_tv = itemView.findViewById(R.id.timer_recommend_tv);
            auto_refresh_tv = itemView.findViewById(R.id.auto_refresh_tv);
        }
    }

}
